<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class BugController extends CommonController {
    //列表
    public function index(){
      $bug_object = D('bug');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($bug_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $bug_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','module');
      $this->assign('umark','bug');
      A('list')->setMetaTitle('漏洞列表')
          ->setBreadCrumb(array('模块','漏洞管理','漏洞列表'))
          ->addTopButton('add',array('title'=>'漏洞添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('company_id','关联厂商','getcompany',100,'false')
          ->addTableColumn('user_id','用户','getuser',80,'false')
          ->addTableColumn('title','标题','text',200,'false')
          ->addTableColumn('type_id','类型','getbugtype',100,'false')
          ->addTableColumn('level','等级','buglevel',80,'false')
          ->addTableColumn('desc','描述','text','auto','false')
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $bug_object = D('bug');
      if (IS_POST) {
          $data = $bug_object->create();
          if ($data) {
              $id = $bug_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'漏洞新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'漏洞新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$bug_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','bug');
        $projectdata = array();
        $pdata = M('project')->field('id,title')->where(array('status'=>1))->select();
        foreach ($pdata as $key => $value) {
            $upp['value'] = $value['id'];
            $upp['title'] = $value['title'];
            array_push($projectdata,$upp);
        }
        $userdata = array();
        $udata = M('user')->field('id,nickname')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $upu['value'] = $value['id'];
            $upu['title'] = $value['nickname'].'（ID:'.$value['id'].'）';
            array_push($userdata,$upu);
        }
        $companydata = array(array('value'=>0,'title'=>'未知厂商'));
        $cdata = M('company')->field('id,name,url')->where(array('status'=>1))->select();
        foreach ($cdata as $key => $value) {
            $upc['value'] = $value['id'];
            $upc['title'] = $value['name'].'-'.$value['url'];
            array_push($companydata,$upc);
        }
        $leveldata = array(
            array('value'=>3,'title'=>'高危'),
            array('value'=>2,'title'=>'中危'),
            array('value'=>1,'title'=>'低危'),
        );
        $typedata = array();
        $tdata = M('bug_type')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($tdata as $key => $value) {
            $upt['value'] = $value['id'];
            $upt['title'] = $value['title'];
            $upt['disabled'] = true;
            array_push($typedata,$upt);
            $subdata = M('bug_type')->field('id,title')->where(array('status'=>1,'upid'=>$value['id']))->select();
            foreach ($subdata as $k => $val) {
                $u['value'] = $val['id'];
                $u['title'] = '· '.$val['title'];
                $u['disabled'] = false;
                array_push($typedata,$u);
            }
        }
        A('form')->setMetaTitle('漏洞添加')
        ->setBreadCrumb(array('模块','模块管理','漏洞列表','漏洞添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('project_id','select','关联项目','',$projectdata)
        ->addFormItem('user_id','select','用户','',$userdata)
        ->addFormItem('title','text','漏洞名称','',array(),array('required'=>true))
        ->addFormItem('company_id','select','关联厂商','',$companydata)
        ->addFormItem('type_id','select','漏洞类型','',$typedata)
        ->addFormItem('level','select','漏洞等级','',$leveldata)
        ->addFormItem('desc','textarea','漏洞描述')
        ->addFormItem('detail','simditor','漏洞详情')
        ->addFormItem('repair','simditor','修复方案')
        ->addFormItem('anonymous','switch','匿名','',array('是','否'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('status'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $bug_object = D('bug');
      if (IS_POST) {
          $data = $bug_object->create();
          if ($data) {
              $rs = $bug_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'漏洞编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'漏洞编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$bug_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','bug');

        $id = I('get.ids',0,'intval');
        $form_data = $bug_object->find($id);
        $projectdata = array();
        $pdata = M('project')->field('id,title')->where(array('status'=>1))->select();
        foreach ($pdata as $key => $value) {
            $upp['value'] = $value['id'];
            $upp['title'] = $value['title'];
            array_push($projectdata,$upp);
        }
        $userdata = array();
        $udata = M('user')->field('id,nickname')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $upu['value'] = $value['id'];
            $upu['title'] = $value['nickname'].'（ID:'.$value['id'].'）';
            array_push($userdata,$upu);
        }
        $companydata = array(array('value'=>0,'title'=>'未知厂商'));
        $cdata = M('company')->field('id,name,url')->where(array('status'=>1))->select();
        foreach ($cdata as $key => $value) {
            $upc['value'] = $value['id'];
            $upc['title'] = $value['name'].'-'.$value['url'];
            array_push($companydata,$upc);
        }
        $leveldata = array(
            array('value'=>3,'title'=>'高危'),
            array('value'=>2,'title'=>'中危'),
            array('value'=>1,'title'=>'低危'),
        );
        $typedata = array();
        $tdata = M('bug_type')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($tdata as $key => $value) {
            $upt['value'] = $value['id'];
            $upt['title'] = $value['title'];
            $upt['disabled'] = true;
            array_push($typedata,$upt);
            $subdata = M('bug_type')->field('id,title')->where(array('status'=>1,'upid'=>$value['id']))->select();
            foreach ($subdata as $k => $val) {
                $u['value'] = $val['id'];
                $u['title'] = '· '.$val['title'];
                $u['disabled'] = false;
                array_push($typedata,$u);
            }
        }
        A('form')->setMetaTitle('漏洞编辑')
        ->setBreadCrumb(array('模块','模块管理','漏洞列表','漏洞编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('project_id','select','关联项目','',$projectdata)
        ->addFormItem('user_id','select','用户','',$userdata)
        ->addFormItem('title','text','漏洞名称','',array(),array('required'=>true))
        ->addFormItem('company_id','select','关联厂商','',$companydata)
        ->addFormItem('type_id','select','漏洞类型','',$typedata)
        ->addFormItem('level','select','漏洞等级','',$leveldata)
        ->addFormItem('desc','textarea','漏洞描述')
        ->addFormItem('detail','simditor','漏洞详情')
        ->addFormItem('repair','simditor','修复方案')
        ->addFormItem('anonymous','switch','匿名','',array('是','否'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $bug_object = M('bug');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $bug_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'漏洞状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'漏洞状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $bug_object = M('bug');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $bug_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'漏洞删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'漏洞删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
